package sg.edu.nus.iss.umpire.service.workflow;

import sg.edu.nus.iss.umpire.dao.jpa.CompetitionDaoImpl;
import sg.edu.nus.iss.umpire.domain.Competition;
import sg.edu.nus.iss.umpire.exception.ServiceException;
import sg.edu.nus.iss.umpire.support.HttpMethod;
import sg.edu.nus.iss.umpire.support.Service;
import sg.edu.nus.iss.umpire.support.ServiceConfig;
import sg.edu.nus.iss.umpire.support.ServiceContext;
import sg.edu.nus.iss.umpire.support.StringUtil;

@ServiceConfig(path = "workflow", method = HttpMethod.GET)
public class WorkflowPageController implements Service{
	
	@Override
	public String serve(ServiceContext ctx) throws Exception {

		String page = ctx.getRequest().getParameter("page");

		if (StringUtil.isEmpty(page)) {
			return index(ctx);
		} else if ("chooseUmpires".equals(page)) {
			return chooseUmpires(ctx);
		} else if ("inviteUmpires".equals(page)) {
			return inviteUmpires(ctx);
		} else if("umpireReply".equals(page)){
			return umpireReply(ctx);
		}

		throw new ServiceException("Invalid parameter");
	}

	private String index(ServiceContext ctx) {
		return null;
	}
	
	private String umpireReply(ServiceContext ctx) {
		fillCompetition(ctx);
		return "competitions/umpire_reply";
	}

	private String chooseUmpires(ServiceContext ctx) {
		fillCompetition(ctx);
		return "competitions/choose_umpires";
	}
	
	private String inviteUmpires(ServiceContext ctx) {
		fillCompetition(ctx);
		return "competitions/umpires_invitation";
	}
	
	private Competition fillCompetition(ServiceContext ctx) {
		String id = ctx.getRequest().getParameter("id");
		Competition competition = new CompetitionDaoImpl(ctx.getEntityManager()).getByKey(Long.valueOf(id));
		ctx.getRequest().setAttribute("competition", competition);
		return competition;
	}
}
